<?php
/**
 * @category Goatherd
 * @package Goatherd\Magic
 *
 * @author Copyright (c) 2012 Maik Penz <maik@phpkuh.de>
 * @version $Id: Entity.php 84 2012-03-27 22:24:33Z maik@phpkuh.de $
 *
 * This file is part of Goatherd library.
 *
 * Goatherd library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Goatherd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Goatherd library. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Goatherd\Magic\Duck;

use Goatherd\Magic\DuckableTrait;

use Goatherd\Commons\Collection\IEntity;

/**
 * Duckable entity.
 *
 * @category Goatherd
 * @package Goatherd\Magic
 * @subpackage Duck
 */
class Entity
implements IEntity
{
    use DuckableTrait;

    /**
     * @todo: improve with real (namespaced) autoloader (loader plugin would be
     * nice to have)
     * @var string
     */
    protected $_pluginNamespace = '';

    /**
     *
     * @param string			$name
     * @return null|object		instance of Goatherd\Magic\Duck.Entity::_duckNamespace
     */
    public function getPlugin($name)
    {
        $pluginClass = $this->_getPluginClass($name);
        if (@class_exists($pluginClass)) {
            $obj = $this->_getPluginInstance($pluginClass);
            return $obj;
        }
    }

    /**
     *
     * @param string			$name
     * @return string
     */
    protected function _getPluginClass($name)
    {
        return $this->_pluginNamespace.ucfirst($name);
    }

    /**
     *
     * @param string			$class
     * @return object|null
     */
    protected function _getPluginInstance($class)
    {
        return new $class();
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__get()
     */
    public function __get($key)
    {
        return $this->getDuck($key);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__set()
     */
    public function __set($key, $value)
    {
        $this->isWritable() and parent::attachDuck($key, $value);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__isset()
     */
    public function __isset($key)
    {
        return $this->getDuck($key) === null;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::__unset()
     */
    public function __unset($key)
    {
        return $this->detachDuck($key);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Commons\Collection.IEntity::getProperties()
     */
    public function getProperties()
    {
        return $this->_duckableObjectPool;
    }
}
